// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   SourceFile

package com.sgame.android.network;

import com.sgame.android.MLog;
import com.sgame.android.lang.*;
import com.sgame.android.lang.Error;

import org.json.*;

// Referenced classes of package com.mobage.ww.android.network:
//            b

public abstract class SgameJsonHttpResponseHandler extends AsyncHttpResponseHandler
{
  private Object mUserData;

  public SgameJsonHttpResponseHandler(Object paramObject)
  {
	  mUserData = paramObject;
  }

  public SgameJsonHttpResponseHandler() {
  }

  public void onSuccess(String paramString) {
    MLog.d("MobageJsonHttpResponseHandler", "onSuccess");
    Object localObject2 = null;
    try
    {
      Object localObject1;
      int i;
      if (((localObject1 = getJsonObject(paramString)) instanceof JSONObject))
      {
        JSONObject localJSONObject;
        if (isSuccess(localJSONObject = (JSONObject)localObject1))
        {
          if (localJSONObject.has("error")) {
            i = localJSONObject.getInt("error");
            localObject2 = localJSONObject.getString("error_msg");
            onFailure(new ServerError(i, (String)localObject2), localJSONObject);
            return;
          }
          if (localJSONObject.has("Error"))
          {
        	  localObject2 = localJSONObject.getJSONObject("Error");
            i = Integer.parseInt(((JSONObject)localObject2).getString("Code"));
            localObject2 = ((JSONObject)localObject2).getString("Message");
            onFailure(new ServerError(i, (String)localObject2), localJSONObject);
            return;
          }
          onFailure(new ServerError(0, localJSONObject.toString()), localJSONObject);
          return;
        }

        onSuccess((JSONObject)localObject1);

        return;
        }
      if ((localObject1 instanceof JSONArray))
    	  onSuccess((JSONArray)localObject1);
      return;
    }
    catch (JSONException localJSONException)
    {
      MLog.e("MobageJsonHttpResponseHandler", "fail to parse the response to JSON Object from the server response: " + paramString);
      onFailure(localJSONException, paramString);
    }
  }

  public void onSuccess(JSONObject paramJSONObject)
  {
    MLog.e("MobageJsonHttpResponseHandler", "onSuccess - got unexpected object.");
    onFailure(new Error(ErrorMap.INVALID_DATA, new RuntimeException("Unexpectedly received an object response")), paramJSONObject);
  }

  public void onSuccess(JSONArray paramJSONArray)
  {
    MLog.e("MobageJsonHttpResponseHandler", "onSuccess - got unexpected array.");
    try
    {
    	onFailure(new Error(ErrorMap.INVALID_DATA, new RuntimeException("Unexpectedly received an object response")), new JSONObject().put("array", paramJSONArray));
    	return;
    }
    catch (JSONException localJSONException) {
    	onFailure(new Error(ErrorMap.INVALID_DATA, new RuntimeException("Unexpectedly received an object response")), new JSONObject());
    }
  }

  public abstract void onFailure(Error paramError, JSONObject paramJSONObject);

  public void onFailure(Throwable paramThrowable, String paramString)
  {
    MLog.d("MobageJsonHttpResponseHandler", "onFailure called with responseBody: " + paramString, paramThrowable);
    Object obj;
    if (paramString != null)
    {
      try
      {
    	  obj = getJsonObject(paramString);
        if ((obj instanceof JSONObject))
        {
        	onFailure(new Error(ErrorMap.SERVER_ERROR), (JSONObject)obj);
          return;
        }
        onFailure(new Error(ErrorMap.SERVER_ERROR), new JSONObject());
        return;
      }
      catch (JSONException ex) {
        MLog.e("MobageJsonHttpResponseHandler", "fail to parse the response to JSON Object from the server response: " + paramString);

        onFailure(new Error(ErrorMap.SERVER_ERROR, paramThrowable), new JSONObject());
        return;
      }
    }

    onFailure(new Error(ErrorMap.NETWORK_UNAVAILABLE, paramThrowable), new JSONObject());
  }

  public final void VerifyData()
  {
	  if ((mUserData instanceof ExecuteHttpRequest));
  }

  private static boolean isSuccess(JSONObject paramJSONObject)
  {
    boolean bool = false;
    try
    {
      if (paramJSONObject.has("success"))
        bool = !paramJSONObject.getBoolean("success");
      else if (paramJSONObject.has("error"))
        bool = paramJSONObject.getBoolean("error");
      else if (paramJSONObject.has("Error")) {
        bool = true;
      }
    }
    catch (JSONException localJSONException)
    {
    }
    return bool;
  }

  private static Object getJsonObject(String paramString) throws JSONException {
    return new JSONTokener(paramString).nextValue();
  }
}
